package com.example.zzl.adapter;

import android.content.Context;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by Administrator on 2016/11/27.
 */
public abstract class TwoMyBaseAdapter<T, V extends TwoMyBaseAdapter.BaseHolder> extends BaseAdapter {

    protected List<T> list;
    public Context context;
    public LayoutInflater inflater;


    private boolean isRecyclerView;

    public TwoMyBaseAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        V v;
        if (convertView == null) {
            v = onCreateViewHolder(parent, getItemViewType(position));
        } else {
            v = (V) convertView.getTag();
          /*  if (isRecyclerView) {
                v = onRecyclerViewHolder(v, parent, getItemViewType(position));
            }*/
        }
        onBindViewHolder(v, position);
        return v.itemView;
    }

    public V onRecyclerViewHolder(V v, ViewGroup parent, int ViewType) {
        return v;
    }

    public abstract static class BaseHolder {
        public final View itemView;
        public ViewDataBinding db;

        private BaseHolder(View itemView) {
            this.itemView = itemView;
            itemView.setTag(this);
        }

        public BaseHolder(ViewDataBinding db) {
            this(db.getRoot());
            this.db = db;
        }
    }

    public abstract V onCreateViewHolder(ViewGroup parent, int viewType);

    public abstract void onBindViewHolder(V holder, int position);

    public boolean isRecyclerView() {
        return isRecyclerView;
    }

    public void setRecyclerView(boolean recyclerView) {
        isRecyclerView = recyclerView;
    }


}
